# -*- coding: utf-8 -*-

#    OriginDB
#    Copyright (C) 2009  Jason Power <power.jg@gmail.com>
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License along
#    with this program; if not, write to the Free Software Foundation, Inc.,
#    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

from PyQt4.QtGui import *
from PyQt4 import QtCore

import sys
sys.path.append('../')

from aeDatabase import Compound, EquCompound, Catalyst, Filter

from ui_filter import Ui_FilterScrollArea

pressureConversions = {"atm":1, "Torr":760, "kPa":101.325}


class FilterWidget(QScrollArea):

	def __init__(self, searchFrame, parent=None):
		QWidget.__init__(self, parent)

		self.searchFrame = searchFrame

		self.reactants = []
		self.products = []
		self.catalysts = []

		# Set up the user interface from Designer.
		self.ui = Ui_FilterScrollArea()
		self.ui.setupUi(self)

		self.ui.pressureUnitsComboBox.addItems(["atm", "Torr", "kPa"])
		self.units = "atm"

		#hacks so I don't have to extend QListWidgetView
		self.ui.reactantListWidget.dropEvent = self.customReactantDropEvent
		self.ui.productListWidget.dropEvent = self.customProductDropEvent
		self.ui.catalystListWidget.dropEvent = self.customCatalystDropEvent

		self.connect(self.ui.pressureUnitsComboBox, QtCore.SIGNAL("currentIndexChanged(const QString &)"), self.pressureUnitChanged)
		self.connect(self.ui.closePushButton, QtCore.SIGNAL("clicked()"), self.parent().closeCurrentTab)
		self.connect(self.ui.clearPushButton, QtCore.SIGNAL("clicked()"), self.clear)


	def getFilter(self):
		filter = Filter()

		if self.ui.temperatureSpinBox.value() != -273:
			filter.temperature = self.ui.temperatureSpinBox.value()
		if self.ui.pressureSpinBox.value() / pressureConversions[self.units] != 0:
			filter.pressure = self.ui.pressureSpinBox.value() / pressureConversions[self.units]

		if self.ui.timeSpinBox.value() != 0:
			filter.time = self.ui.timeSpinBox.value()
		
		if self.ui.pHSpinBox.value() != 0:
			filter.pH = self.ui.pHSpinBox.value()
		if str(self.ui.solventLineEdit.text()):
			filter.solvent = str(self.ui.solventLineEdit.text())
		if self.ui.solidCheckBox.isChecked():
			filter.solid = True
		if self.ui.liquidCheckBox.isChecked():
			filter.liquid = True
		if self.ui.gasCheckBox.isChecked():
			filter.gas = True
		if str(self.ui.otherLineEdit.text()):
			filter.other = str(self.ui.otherLineEdit.text())

		if self.ui.tempMaxRadioButton.isChecked():
			filter.maxtemperature = True
		elif self.ui.tempMinRadioButton.isChecked():
			filter.maxtemperature = False
			
		if self.ui.pressureMaxRadioButton.isChecked():
			filter.maxpressure = True
		elif self.ui.pressureMinRadioButton.isChecked():
			filter.maxpressure = False
			
		if self.ui.timeMaxRadioButton.isChecked():
			filter.maxtime = True
		elif self.ui.timeMinRadioButton.isChecked():
			filter.maxtime = False

		if self.ui.pHMaxRadioButton.isChecked():
			filter.maxpH = True
		elif self.ui.pHMinRadioButton.isChecked():
			filter.maxpH = False

		filter.reactants = self.reactants

		filter.products = self.products

		filter.catalysts = self.catalysts

		return filter


	def clear(self):
		self.reactants = []
		self.products = []
		self.catalysts = []

		self.ui.reactantListWidget.clear()
		self.ui.productListWidget.clear()
		self.ui.catalystListWidget.clear()

		self.ui.temperatureSpinBox.setValue(-273)
		self.ui.pressureSpinBox.setValue(0)
		self.ui.timeSpinBox.setValue(0)
		self.ui.pHSpinBox.setValue(0)

		self.ui.solventLineEdit.clear()
		self.ui.otherLineEdit.clear()

		self.ui.solidCheckBox.setChecked(False)
		self.ui.liquidCheckBox.setChecked(False)
		self.ui.gasCheckBox.setChecked(False)


	def pressureUnitChanged(self, units):
		units = str(units)
		pressure = float(self.ui.pressureSpinBox.value()) / pressureConversions[self.units]
		pressure *= pressureConversions[units]
		self.units = units
		self.ui.pressureSpinBox.setValue(pressure)

	def customReactantDropEvent(self, event):
		obj = self.searchFrame.getSelectedObject()
		if type(obj) == Compound or type(obj) == EquCompound:
			self.reactants.append(obj)
			self.ui.reactantListWidget.addItem(obj.name)

	def customProductDropEvent(self, event):
		obj = self.searchFrame.getSelectedObject()
		if type(obj) == Compound or type(obj) == EquCompound:
			self.products.append(obj)
			self.ui.productListWidget.addItem(obj.name)

	def customCatalystDropEvent(self, event):
		obj = self.searchFrame.getSelectedObject()
		if type(obj) == Catalyst:
			self.catalysts.append(obj)
			self.ui.catalystListWidget.addItem(obj.name)